﻿$(document).ready(function () {
    var date = new Date();
    var currentMonth = date.getMonth();
    var currentYear = date.getFullYear();

    function drawCalendar(month, year) {
        var data = {
            month: month,
            year: year
        };
        $("#calendar").calendarWidget(data);

        //get all the Filming Days of the specified month
        $.post(pathPrefix + "FilmingDays/GetFilmingDaysOfMonth", data, function (result) {
            //loop through the filming days
            for (var i = 0; i < result.length; i++) {
                var date = result[i].DateVal;
                var dayOfMonth = parseInt(date.substring(0, 2));
                var dateCell = $(".current-month.day" + dayOfMonth).addClass("highlighted");

                var albumLink = "<a href='Albums/Details/" + result[i].AlbumId + "'>אלבום: " + result[i].AlbumTitle + "<br />צלם: " + result[i].Photographer + "</a>";
                var title = dateCell.attr("title");

                if (title != undefined) {
                    //append the AlbumTitle and Photographer name to the current cell's title
                    dateCell.attr("title", title + albumLink);
                }
                else {
                    //put the AlbumTitle and Photographer name in the cell's title
                    dateCell.attr("title", albumLink);
                }
            }

            //add the filmingDays to the filming days table
            $("#filmingDaysList table tr:not(:first)").remove();
            $("#filmingDayTemplate").tmpl(result).appendTo($("#filmingDaysList table"));
            tableAltRows();

            //if there are no filmingDays
            if (result.length == 0) {
                $("#filmingDaysList table").hide();
                $("#noFilmingDaysLabel").show();
            }
            else {
                $("#filmingDaysList table").show();
                $("#noFilmingDaysLabel").hide();
            }

            $(".current-month.highlighted[title]").tooltip({ effect: 'slide', predelay: 200, offset: [20, 0] });
        }, "json");
    }

    drawCalendar(currentMonth, currentYear);

    //when clicking on the Previous Month
    $("#prevMonth").live("click", function () {
        currentMonth--;

        if (currentMonth == -1) {
            currentMonth = 11;
            currentYear--;
        }

        drawCalendar(currentMonth, currentYear);
    });

    //when clicking on the Next Month
    $("#nextMonth").live("click", function () {
        currentMonth++;

        if (currentMonth == 12) {
            currentMonth = 0;
            currentYear++;
        }

        drawCalendar(currentMonth, currentYear);
    });

    //when changing the selected view (list or calendar)
    $("#CalendarView").change(function () {
        $("#calendar").hide();
        $("#filmingDaysList").hide();

        var selectedView = $(this).val();
        $("#" + selectedView).show();
    });
});